Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use owner refs to determine ownership for ASO resources #4499

Merged
merged 1 commit into from
Jan 24, 2024

Conversation

nojnhuh
Copy link
Contributor

@nojnhuh nojnhuh commented Jan 23, 2024

What type of PR is this?
/kind feature

What this PR does / why we need it:

This PR makes CAPZ add an OwnerReference to each ASO resource it controls (i.e. any ASO resource not pre-created by a user). This is used instead of the sigs.k8s.io_cluster-api-provider-azure_owned label that's currently used. This better enables #4338 by tracking ownership more granularly than only by Cluster (such as per-AzureMachine or per-AzureManagedMachinePool) and #4339. More motivation is spelled out in #4340.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #4340

Special notes for your reviewer:

TODOs:

  • squashed commits
  • includes documentation
  • adds unit tests

Release note:

CAPZ now uses `metadata.ownerReferences` instead of the `sigs.k8s.io_cluster-api-provider-azure_owned` label to track ownership of ASO resources

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 23, 2024
Comment on lines +161 to +162
//
// Deprecated: OwnerReferences now determine ownership.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax makes sure the linter flags any additional usages of this constant.

Comment on lines +155 to +157
// ResourceRef returns a concrete, named ASO resource type to facilitate a
// strongly-typed GET. Namespace is not read if set here and is instead
// derived from OwnerReferences.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OwnerReferences can't span namespaces, so this is why namespace is being removed in lots of places throughout this PR.

if !ownedByCluster(existing.GetLabels(), r.clusterName) {
if isOwned, err := isOwnedBy(resource, r.owner, r.Scheme()); err != nil {
return zero, err
} else if !isOwned && !hasLegacyOwnedByLabel(resource.GetLabels(), r.clusterName) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should fulfill back-compat.

Copy link

codecov bot commented Jan 23, 2024

Codecov Report

Attention: 25 lines in your changes are missing coverage. Please review.

Comparison is base (166a59d) 62.11% compared to head (6b3969c) 62.16%.
Report is 6 commits behind head on main.

Files Patch % Lines
azure/services/aso/aso.go 73.91% 6 Missing and 6 partials ⚠️
azure/scope/cluster.go 0.00% 2 Missing ⚠️
azure/scope/managedmachinepool.go 0.00% 2 Missing ⚠️
azure/services/agentpools/spec.go 0.00% 1 Missing ⚠️
azure/services/aso/service.go 50.00% 1 Missing ⚠️
azure/services/bastionhosts/spec.go 0.00% 1 Missing ⚠️
azure/services/fleetsmembers/spec.go 0.00% 1 Missing ⚠️
azure/services/managedclusters/spec.go 0.00% 1 Missing ⚠️
azure/services/natgateways/spec.go 0.00% 1 Missing ⚠️
azure/services/privateendpoints/spec.go 0.00% 1 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4499      +/-   ##
==========================================
+ Coverage   62.11%   62.16%   +0.05%     
==========================================
  Files         189      189              
  Lines       18639    18633       -6     
==========================================
+ Hits        11577    11584       +7     
+ Misses       6425     6410      -15     
- Partials      637      639       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +161 to +163
if err := controllerutil.SetControllerReference(r.owner, parameters, r.Client.Scheme()); err != nil {
return zero, errors.Wrap(err, "failed to set owner ref")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we're not adding the owner ref only on create like were adding the label before is because we want to set the owner ref also on existing resources during a CAPZ upgrade.

@jackfrancis
Copy link
Contributor

This lgtm. Do we have any additional BYO E2E scenarios we can light up to validate the various flavors of owner-ness?

@nojnhuh
Copy link
Contributor Author

nojnhuh commented Jan 23, 2024

The optional tests do some BYO things. Otherwise we're tracking fleshing out these kinds of tests more in #3545.

/test pull-cluster-api-provider-azure-e2e-optional

@@ -422,6 +422,7 @@ func (s *ManagedControlPlaneScope) IsVnetManaged() bool {
defer done()

vnet := s.VNetSpec().ResourceRef()
vnet.SetNamespace(s.ASOOwner().GetNamespace())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the namespace set here explicitly? I see that other resources have the namespace set in the generic aso resource, but I could be misunderstanding how that works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on the interface definition for ResourceRef() now explains that it no longer reads the namespace from there and instead uses the namespace of the owner. This is to account for that change. On the plus side, we won't have to add a namespace field to the Spec types for all the services we have yet to convert to ASO.

Namespace: namespace,
Labels: map[string]string{
infrav1.OwnedByClusterLabelKey: "not-" + clusterName,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep a test case that uses the legacy label to test back compat?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That scenario is already tested in the aso package, so I'm not sure if it's worth duplicating that here.

Copy link
Contributor

@willie-yao willie-yao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 23, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 71306c7fbc6ba18e49c581d8c1a77363fe2708d7

Copy link
Contributor

@mboersma mboersma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@jackfrancis
Copy link
Contributor

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jackfrancis

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 23, 2024
@nawazkh
Copy link
Member

nawazkh commented Jan 24, 2024

/lgtm

@k8s-ci-robot k8s-ci-robot merged commit aa9af62 into kubernetes-sigs:main Jan 24, 2024
21 checks passed
@nojnhuh nojnhuh deleted the aso-ownerref branch January 24, 2024 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Set a controller: true OwnerReference on ASO resources referring to its managing CAPZ resource
6 participants